package com.link.base.base.sms.controller;


import com.link.base.base.sms.model.SendSmsAccount;
import com.link.base.base.sms.service.SendSmsAccountService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模板消息-客户中间表Controller
 * @Author: liulu
 * @Date:15:37 2018/11/5
 */
@Controller
@RequestMapping("/link/sendSmsAccount")
public class SendSmsAccountController extends BasicController<SendSmsAccount> {

    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private SendSmsAccountService sendSmsAccountService;

    public SendSmsAccountController(KeyGenerateService keyGenerateService) {
        this.keyGenerateService = keyGenerateService;
    }

    @Override
    public BasicService<SendSmsAccount> getBasicService() throws Exception {
        return sendSmsAccountService;
    }

    /**
     * 根据条件查询记录,默认分页(若将'pageFlag'设置为'false'，则不分页)
     */
    @Override
    @RequestMapping(value = "/queryByExamplePage")
    @ResponseBody
    public Map<String, Object> queryByExamplePage(@JsonParam QueryParams qps,@JsonParam  SendSmsAccount entity, HttpSession session,
                                                  HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(8);
        try {
            qps.preDealSecurity(request);
            SendSmsAccount t = (SendSmsAccount) BasicModel.transformClass(entity, qps);
            beforQueryAllOrExample(t, request);
            beforQueryExample(t, request);
            List<SendSmsAccount> list = getBasicService().queryByExamplePage(t);
            for (SendSmsAccount send : list) {
                String mo = send.getMoText();
                if (send.getParameter1() != null && !"".equals(send.getParameter1())) {
                    mo = mo.replace("{1}", send.getParameter1());
                }
                if (send.getParameter2() != null && !"".equals(send.getParameter2())) {
                    mo = mo.replace("{2}", send.getParameter2());
                }
                if (send.getParameter3() != null && !"".equals(send.getParameter3())) {
                    mo = mo.replace("{3}", send.getParameter3());
                }
                if (send.getParameter4() != null && !"".equals(send.getParameter4())) {
                    mo = mo.replace("{4}", send.getParameter4());
                }
                if (send.getParameter5() != null && !"".equals(send.getParameter5())) {
                    mo = mo.replace("{5}", send.getParameter5());
                }
                if (send.getParameter6() != null && !"".equals(send.getParameter6())) {
                    mo = mo.replace("{6}", send.getParameter6());
                }
                if (send.getParameter7() != null && !"".equals(send.getParameter7())) {
                    mo = mo.replace("{7}", send.getParameter7());
                }
                if (send.getParameter8() != null && !"".equals(send.getParameter8())) {
                    mo = mo.replace("{8}", send.getParameter8());
                }
                if (send.getParameter9() != null && !"".equals(send.getParameter9())) {
                    mo = mo.replace("{9}", send.getParameter9());
                }
                if (send.getParameter10() != null && !"".equals(send.getParameter10())) {
                    mo = mo.replace("{10}", send.getParameter10());
                }

                send.setMoText(mo);
            }
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount(t, list.size()));
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<SendSmsAccount>());
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<SendSmsAccount>());
            result.put("result", e.getMessage());
        }
        return result;
    }
}
